package com.qcby.service.impl;

import com.qcby.dao.PersonalCollectionDao;
import com.qcby.entity.PersonalFavoritesFolder;
import com.qcby.entity.Result;
import com.qcby.entity.Shop;
import com.qcby.service.PersonalCollectionService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PersonalCollectionServiceImpl implements PersonalCollectionService {

    @Autowired
    private PersonalCollectionDao personalCollectionDao;

    @Override
    public Result<Integer> getUserIdByName(String username) {
        return Result.success(personalCollectionDao.getUserIdByName(username));
    }

    @Override
    public Result<List<PersonalFavoritesFolder>> selectFavoritesList(Integer  u_id,Integer fatherFolderId) {
        System.out.println("u_id = " + u_id);
        System.out.println("personalCollectionDao.selectFavoritesList(u_id,fatherFolderId)" );
        System.out.println(personalCollectionDao.selectFavoritesList(u_id,fatherFolderId).toString());
        //int count = personalCollectionDao.getFavoriteNumInFolder(u_id,fatherFolderId);
//        int count = personalCollectionDao.getFavoriteNumInFolder(u_id,fatherFolderId);
        return Result.success(personalCollectionDao.selectFavoritesList(u_id,fatherFolderId));
    }

    @Override
    public Result<List<Shop>> selectFavoriteShop(int u_id,int folderId) {
        System.out.println(personalCollectionDao.selectFavoriteShop(u_id,folderId));
        return Result.success(personalCollectionDao.selectFavoriteShop(u_id,folderId));
    }

    @Override
    public Result<PersonalFavoritesFolder> createFolder(PersonalFavoritesFolder folderInfo) {
        int num = personalCollectionDao.createFolder(folderInfo);

        return (num==1)?Result.success(folderInfo):Result.error("收藏夹创建失败...");
    }

    @Override
    public Result<Integer> collection(Integer userId, Integer shopId, String reason, Integer folderId) {
        // 1. 校验核心参数非空
        if (userId == null || shopId == null) {
            return Result.error("用户ID或店铺ID为空，无法收藏");
        }
        // 2. 执行Dao插入
        int rows = personalCollectionDao.collection(userId, shopId, reason, folderId);
        //System.out.println("收藏操作影响行数：" + rows);
        return rows > 0 ? Result.success(rows) : Result.error("收藏失败");
    }

    @Override
    public Result<Integer> deleteFolder(int shopId, int u_id, int folderId) {
        return Result.success(personalCollectionDao.deleteFavoriteShop(shopId, u_id, folderId));
    }

    @Override
    public Result<Integer> recollecteFolder(int shopId, int u_id, int folderId) {
        return Result.success(personalCollectionDao.recollectFavoriteShop(shopId, u_id, folderId));
    }

    @Override
    public Result<List<PersonalFavoritesFolder>> updateShopCountInFolder(Integer userId, Integer fatherId) {
        List<PersonalFavoritesFolder> firstLevelFolders = personalCollectionDao.findFolderByFatherId(userId, fatherId);
        if (firstLevelFolders == null) {
            firstLevelFolders = new ArrayList<>();
        }
        for (PersonalFavoritesFolder folder : firstLevelFolders) {
            // 2. 递归计算当前文件夹的总店铺数
            int totalShopCount = calculateTotalShopCount(folder.getId(), userId);
            // 3. 更新数据库中该文件夹的 shop_count
            personalCollectionDao.updateShopCount(userId, folder.getId(), totalShopCount);
        }


        return Result.success(personalCollectionDao.findFolderByFatherId(userId, fatherId));
    }

    private int calculateTotalShopCount(int folderId, int userId) {
        // 1. 获取当前文件夹自身的店铺数
         int selfShopCount = personalCollectionDao.findShopCountInFolderById(folderId, userId);

//        System.out.println("---selfShopCount = " + selfShopCount);

        // 2. 查询当前文件夹的所有直接子文件夹
        List<PersonalFavoritesFolder> childFolders = personalCollectionDao.findFolderByFatherId(userId, folderId);

        // 3. 递归计算所有子文件夹的总店铺数，并累加
        int childTotalShopCount = 0;
        for (PersonalFavoritesFolder child : childFolders) {
            childTotalShopCount += calculateTotalShopCount(child.getId(), userId);
//            System.out.println("---childTotalShopCount = " + childTotalShopCount);
        }

        // 4. 返回“自身店铺数 + 所有子文件夹总店铺数”
        return selfShopCount + childTotalShopCount;
    }
}
